<?php
/**资料管理
 * Created by leiying
 * Date: 2018-06-05
 * Time: 14:30:21
 */

namespace Home\Controller;


use Think\Controller;

class DataController extends Controller
{
    /**查询完成下载申请表格
     * @author leiying
     */
    public function getComplete(){
        $id = I('get.id');
        if(empty($id)){
            $this->error("异常错误，如有疑问请联系客服",U('Home/Index/index'),3);
        }
        $localhost = $_SERVER [ 'HTTP_HOST' ];
        $a_path = "/Uploads/PDF/Admin/";
        $path = $_SERVER['DOCUMENT_ROOT']. $a_path;
        if(!file_exists($path))
        {
            mkdir($path,0777,true);
        }

        $filename = M("apply")->where(array("id"=>$id))->getField("number");
        if(!$filename){$this->error("异常错误，如有疑问请联系客服",U('Home/Index/index'),3);}

        $url = U("Admin/Template/complete",array("id"=>$id));

//        if(!file_exists($path."{$filename}.pdf")){
//            shell_exec("D:\app\wkhtmltopdf\bin\wkhtmltopdf.exe {$localhost}{$url} {$path}{$filename}.pdf");
            shell_exec("D:\leiying\wkhtmltopdf\bin\wkhtmltopdf.exe {$localhost}{$url} {$path}{$filename}.pdf");
//        }

        if(file_exists($path."{$filename}.pdf")){
            header("Content-type:application/pdf");
            header("Content-Disposition:attachment;filename={$filename}.pdf");
            echo file_get_contents("{$path}{$filename}.pdf");
        }else{
            $this->error('异常错误，如有疑问请联系客服',U('Home/Index/index'),3);
            exit;
        }
    }

    /**下载资源
     * @author leiying
     */
    public function download(){
        $method = I('method','data');
        $id = I('id',0);
        switch($method){
            case 'data':
                $res = M("data")->field("path,name,ext")->where(array("id"=>$id))->find();
                $path = isset($res['path'])?$res['path']:'';
                $name = isset($res['name'])?$res['name']:'';
                $ext = isset($res['ext'])?$res['ext']:'';
                break;
            case 'apply':
                $res = M("apply")->field("path,number name")->where(array("id"=>$id))->find();
                $path = isset($res['path'])?$res['path']:'';
                $name = isset($res['name'])?$res['name']:'';
                $ext = pathinfo($path)["extension"];
                break;
            default;
                $this->error("参数错误，请联系开发人员");
                break;
        }
        download_file($path,$name.'.'.$ext);
    }

    /**上传资源文件
     * @param $files
     * @param $path  尾部加上"/"
     * @return string  返回存入数据表的路径
     * @author leiying
     */
    protected function upload_file($files,$path,$file_name = 'upfile'){
        $uptypes=array(//word excel pdf jpg png rar zip
            'application/vnd.ms-excel',//xls
            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',//xlsx
            'application/msword',//doc
            'application/vnd.openxmlformats-officedocument.wordprocessingml.document',//docx
            'application/x-zip-compressed',//zip
            'application/octet-stream',//rar
            'application/pdf',//pdf
            'image/jpg', 'image/jpeg', 'image/png', 'image/pjpeg', 'image/gif', 'image/bmp', 'image/x-png'
        );//上传文件类型列表
        $max_file_size = 200*1024*1024;     //上传文件大小限制, 单位BYTE
        $destination_folder=$_SERVER['DOCUMENT_ROOT'].$path; //上传文件路径
        if (!is_uploaded_file($files[$file_name]["tmp_name"])){   //防止脚本欺骗
            $this->error('图片不存在');
        }

        $file = $files["upfile"];
        if($max_file_size < $file["size"]){   //检查文件大小
            $this->error('文件太大');
        }

        if(!in_array($file["type"], $uptypes)){   //检查文件类型
            $this->error("文件类型不符!".$file["type"]);
        }

        if(!file_exists($destination_folder))
        {
            mkdir($destination_folder,0777,true);
        }

        $filename=$file["tmp_name"];
//        $image_size = getimagesize($filename);
        $pinfo=pathinfo($file["name"]);
        $ftype=$pinfo['extension'];
        $destination = $destination_folder.md5(time().mt_rand(1000, 9999)).".".$ftype;
        if (file_exists($destination))
        {
            $this->error("同名文件已经存在了");
        }

        if(!move_uploaded_file ($filename, $destination))
        {
            $this->error($filename."移动到".$destination."出错");
        }

        $pinfo=pathinfo($destination);
        $fname=$pinfo["basename"];
        return $path.$fname;
    }


}